package com.fzrskj.cloud.free.core.utils;

import com.fzrskj.cloud.free.core.enums.EncryptEnum;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.winiis.component.common.utils.encrypt.Encrypt3DESUtils;
import com.winiis.component.common.utils.encrypt.EncryptAESUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * EncryptAESUtils.
 *
 * @author zj.
 *         Created on 2018/11/29 0029.
 */
public class EncryptUtils {
    /**
     * 公共隐私数据加密.
     * @param source 待加密数据.
     * @return Base64的密文
     */
    public static String encodePublic(String source) {
        String sKey = ConfigPropUtils.getEncryptKeyCustomer();
        return encryptECB(source, sKey);
    }

    /**
     * 公共隐私数据解密.
     * @param encode Base64的密文
     * @return 解密明文.
     */
    public static String decodePublic(String encode) {
        String sKey = ConfigPropUtils.getEncryptKeyCustomer();
        return decryptECB(encode, sKey);
    }

    /**
     * 会话隐私数据加密.
     * @param source 待加密数据.
     * @return Base64的密文
     */
    public static String encodeSession(String source) {
        String sKey = CloudContextUtils.getSK();
        return encryptECB(source, sKey);
    }

    /**
     * 会话隐私数据解密.
     * @param encode 待解密数据.
     * @return 解密结果.
     */
    public static String decodeSession(String encode) {
        String sKey = CloudContextUtils.getSK();
        return decryptECB(encode, sKey);
    }

    /**
     * ECB ZeroPadding加密（UTF-8）
     * @param source 明文.
     * @param sKey 密钥.
     * @return 密文.
     */
    public static String encryptECB(String source, String sKey) {
        return EncryptAESUtils.encrypt(source, sKey, EncryptAESUtils.MODE_ECB_NO_PADDING);
    }

    /**
     * ECB ZeroPadding 解密.
     * @param encode 密文.
     * @param sKey 密钥.
     * @return 解密明文.
     */
    public static String decryptECB(String encode, String sKey) {
        return EncryptAESUtils.decrypt(encode, sKey, EncryptAESUtils.MODE_ECB_NO_PADDING);
    }

    private static String akName = "ak";
    private static String skName = "sk";

    /**
     * 设置公共信息密钥.
     * @param response HttpServletResponse
     */
    public static void makePublic(HttpServletResponse response) {
        try {
            String key = encryptECB(ConfigPropUtils.getEncryptKeyCustomer(), EncryptEnum.BASE.getKey());
            key = URLEncoder.encode(key, "utf-8");
            //特殊处理
            Cookie ck = new Cookie(akName, key);
            ck.setPath("/");
            ck.setHttpOnly(false);
            response.addCookie(ck);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置会话信息密钥
     * @param response HttpServletResponse
     * @param request HttpServletRequest
     */
    public static void makeSession(HttpServletResponse response, HttpServletRequest request) {
        try {
            String key = CStrTool.randStr(16);
            request.getSession().setAttribute(skName, key);

            key = encryptECB(key, EncryptEnum.BASE.getKey());
            key = URLEncoder.encode(key, "utf-8");
            //特殊处理
            Cookie ck = new Cookie(skName, key);
            ck.setPath("/");
            ck.setHttpOnly(false);
            response.addCookie(ck);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取会话信息密钥.
     * @param request HttpServletRequest
     * @return
     */
    public static String getSession(HttpServletRequest request) {
        return String.valueOf(request.getSession().getAttribute(skName));
    }

    /**
     * 存储数据加密码.
     * @param source 源.
     * @return encode string.
     */
    public static String encodeSave(String source) {
        return Encrypt3DESUtils.encrypt(source, ConfigPropUtils.getEncryptKeyService());
    }

    /**
     * 存储数据解密.
     * @param source 源.
     * @return decode string.
     */
    public static String decodeSave(String source) {
        return Encrypt3DESUtils.decrypt(source, ConfigPropUtils.getEncryptKeyService());
    }

    public static void main(String[] args) {
        String test = "wxc2ee3b5b9c821cc0";
        String en = encodeSave(test);
        String de = decodeSave("3fedd9fce1419d67bdb833e4988e31d7edbb0858577d91dc");
        System.out.println(de);
    }
}
